
#include "img_queue.h"

static img_manage_t  img_q;

int img_queue_init(void)
{
    img_q.len = 0;

    img_q.e_head = (slist_t *)malloc(sizeof(slist_t));
	if (img_q.e_head == NULL) {
		l_d("e_head null\n");
		return -1;
	}
    slist_init(img_q.e_head);
    return 0;
}

int img_queue_destroy(void)
{
	int ret = 0;
	slist_t* node;
	img_link_t * obj = NULL;

	if(!img_q.e_head){
		return -1;
	}
	//***********************************************/
	slist_for_each(node,(img_q.e_head))
	{
		obj = slist_entry(node,img_link_t,e_tail);
		if(obj)
		{
			slist_remove(img_q.e_head, &(obj->e_tail));
            free(obj);
            obj = NULL;
			img_q.len--;
		}
	}
	//***********************************************/
	if(img_q.e_head){
		free(img_q.e_head);
		img_q.e_head = NULL;
	}
	//***********************************************/

    return 0;
}

int img_queue_add(const char* name)
{
    img_link_t * obj = NULL;

	if(!img_q.e_head){
		return -1;
	}
	//**************************************************
    obj = (img_link_t *)malloc(sizeof(img_link_t));
    if(obj == NULL){
        l_d("obj err\n");
        return -1;
    }
    memset(obj, 0, sizeof(img_link_t));
    slist_init(&(obj->e_tail));
    //***********************************************************
	snprintf(obj->e_attr.name,sizeof(obj->e_attr.name),"%s",name);
	obj->e_attr.img_dsc = NULL;
    //***********************************************************
    img_q.len++;
    slist_add_tail(img_q.e_head,&(obj->e_tail));
    //**************************************************

    return 0;
}

int img_queue_del(const char* name)
{
	int ret = 0;
	slist_t* node;
	img_link_t * obj = NULL;
	//**************************************************
	if(!img_q.e_head){
		return -1;
	}
	//**************************************************
	if(slist_isempty(img_q.e_head)){
		return -1;
	}
	//**************************************************
	slist_for_each(node, (img_q.e_head))
	{
		obj = slist_entry(node, img_link_t, e_tail);
		if((obj)&&(!strcmp(obj->e_attr.name,name)))
		{
             
            ret = 1;
            break;
		}
	}
	//**************************************************
	if(ret){
		slist_remove(img_q.e_head, &(obj->e_tail));
		img_q.len--;

        if(obj){
            free(obj);
            obj = NULL;
        }
	}
	//**************************************************

	return 0;
}

int img_queue_trav(void)
{
    int i = 0;
	slist_t* node;
    img_link_t * obj = NULL;
	//**************************************************
	if(!img_q.e_head){
		return -1;
	}
	//**************************************************
	if(slist_isempty(img_q.e_head)){
		return -1;
	}

	slist_for_each(node,(img_q.e_head))
	{
		obj = slist_entry(node,img_link_t,e_tail);
		if(obj){
			#if 0
			l_d("i:n=%d:%s\n",i,obj->e_attr.name);
			#endif
            i++;
		}
	}

	return 0;
}

int img_queue_get_len(void)
{
    l_d("len=%d\n",img_q.len);

    return img_q.len;
}

int img_getFileSize(const char* fileName) {
    struct stat st;
    if (stat(fileName, &st) == -1) {
        return -1;  // 返回-1表示获取文件大小失败
    }
    return st.st_size;
}

int img_queue_decode_all(void)
{
    int i = 0;
	int ret = -1;
	slist_t* node;
    img_link_t * obj = NULL;
	char new_path[1024];
	
	int fp = -1;
	unsigned int w, h;
	uint32_t sbuf_len = 0;
	unsigned char* dbuf = NULL;
	unsigned char* sbuf = NULL;
	lv_img_header_t header;

	unsigned char ucdata;
	unsigned char* p = NULL;
	int32_t t, j;

	int32_t total_size = 0;
	//**************************************************
	if(!img_q.e_head){
		return -1;
	}
	//**************************************************
	if(slist_isempty(img_q.e_head)){
		return -1;
	}
	//**************************************************
	slist_for_each(node,(img_q.e_head))
	{
		obj = slist_entry(node,img_link_t,e_tail);
		if(!obj){
			continue;
		}
		#if 0
		l_d("i:n=%d:%s\n",i++,obj->e_attr.name);
		#endif
		//***************************************************************
		obj->e_attr.img_dsc = (lv_img_dsc_t *)malloc(sizeof(lv_img_dsc_t));
		if(!obj->e_attr.img_dsc){
			continue;
		}
		//***************************************************************
		memset(new_path,0,sizeof(new_path));
		snprintf(new_path,sizeof(new_path),"L:%s",obj->e_attr.name);
    	lv_img_decoder_get_info(new_path, &header);

		sbuf_len = img_getFileSize(obj->e_attr.name);
		
		sbuf = malloc(sbuf_len);
		if(!sbuf){
			continue;
		}

		fp = open(obj->e_attr.name,O_RDWR);
		if(fp < 0){
			free(sbuf);
			sbuf = NULL;
			continue;
		}

		read(fp,sbuf,sbuf_len);
		close(fp);
		fp = -1;

		ret = lv_png_get_argb_from_buff(&dbuf,sbuf,sbuf_len,&w,&h);
		if(ret){
			free(sbuf);
			sbuf = NULL;
			continue;
		}

		t = w * h;
		p = dbuf;
		// for (j = 0; j < t; j++)
		// {
		// 	ucdata = p[0];
		// 	p[0] = p[2];
		// 	p[2] = ucdata;
		// 	p += 4;
		// }
		
   		obj->e_attr.img_dsc->header.cf  		= header.cf;
    	obj->e_attr.img_dsc->header.always_zero = header.always_zero;
    	obj->e_attr.img_dsc->header.reserved    = header.reserved;
    	obj->e_attr.img_dsc->header.w  			= header.w;
    	obj->e_attr.img_dsc->header.h 			= header.h;
    	obj->e_attr.img_dsc->data_size			= header.w*header.h*4;
    	obj->e_attr.img_dsc->data 				= (const uint8_t*)dbuf;

		total_size = total_size + header.w*header.h*4;

		free(sbuf);
		sbuf = NULL;
		//****************************************************************/
		// memset(new_path,0,sizeof(new_path));
		// snprintf(new_path,sizeof(new_path),"%s.dsc",obj->e_attr.name);
		// l_d("new=%s\n",new_path);

		// fp = open(new_path,O_WRONLY|O_CREAT);
		// if(fp < 0){
		// 	l_d("err\n");
		// 	continue;
		// }

		// write(fp,obj->e_attr.img_dsc,sizeof(lv_img_dsc_t));
		// write(fp,dbuf,header.w*header.h*4);
		// close(fp);
		// fp = -1;
		//****************************************************************/
	}

	l_d("total_size=%dk\n",total_size/1024);

	return 0;
}


void * img_queue_get_byname(const char * name)
{
    int i = 0;
	slist_t* node;
    img_link_t * obj = NULL;
	void * img_p = NULL;
	//**************************************************
	if(!img_q.e_head){
		return NULL;
	}
	//**************************************************
	if(slist_isempty(img_q.e_head)){
		return NULL;
	}
	//**************************************************
	slist_for_each(node,(img_q.e_head))
	{
		obj = slist_entry(node,img_link_t,e_tail);
		if((obj)&&(strstr(obj->e_attr.name,name)))
		{
			img_p = (void *)obj->e_attr.img_dsc;
		}
	}
	//**************************************************

	return img_p;
}
